<?php
class helper_users_point
{
	/**
	 * 
	 * @return 赠送兑现
	 * @var Userbase
	 */
	protected $souce_user = null;
	/**
	 * 
	 * @return 被赠送对象
	 * @var Userbase
	 */
	protected $target_user = null;
	
	/**
	 * 
	 * @return 赠送积分
	 * @var int
	 */
	protected $point = null;
	/**
	 * 
	 * @return 手续费比例
	 * @var int
	 */
	protected $scale = 100;
	
	
	public $errmsg = '';
	/**
	 * 
	 *2012-1-2-下午04:07:34 by 460932465
	 * Enter description here ...
	 * @param userbase $source_user
	 */
	function __construct(&$source_user)
	{
		$this->souce_user = $source_user;
	}
	/**
	 * 
	 *2012-1-2-下午04:08:21 by 460932465
	 * @return 开始赠送前检测
	 */
	function _befor_send()
	{
		if (intval($this->point)<=0)
		{
			$this->errmsg = '对不起,赠送的积分点为0';
			return false;
		}else
		{
			$this->point = intval(((($this->scale)/1000)+1)*$this->point);
			if ($this->souce_user->userpoint < $this->point)
			{
				$this->errmsg = '对不起,您的积分余额不足';
				return false;
			}else if(!$this->target_user->id())
			{
				$this->errmsg = '对不起,您要赠送的用户不存在';
				return false;
			}else
			{
				return true;
			}
		}
	}
	function _after_send()
	{
		return true;
	}
	function send($target_phoneNum,$point)
	{
		$this->target_user = Userbase::find('userid = ?',$target_phoneNum)->getOne();
		$this->point = intval($point);
		if ($this->_befor_send())
		{
			QDB::getConn()->startTrans();
			try {
				//先扣
				$this->souce_user->userpoint = $this->souce_user->userpoint - $this->point;
				$this->souce_user->save(0,'update');
				//增加
				$this->target_user->userpoint = $this->target_user->userpoint + $point;
				$this->target_user->save(0,'update');
				QDB::getConn()->completeTrans(true);
				
			} catch (Exception $e) {
				QDB::getConn()->completeTrans(false);
				$this->errmsg = '数据更改失败';
				return false;
			}
			return $this->_after_send();
		}
	}
	
	
	
}

?>